package com.itheima.edu.info.manager.service;

import com.itheima.edu.info.manager.dao.BaseStudentDao;
import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.factory.StudentDaoFactory;

import java.io.FileNotFoundException;

public class StudentService {
    //创建StudentDao(库管)
    //private OtherStudentDao studentDao = new OtherStudentDao();

    //通过学生库管工厂类,获取库管对象
    private BaseStudentDao studentDao = StudentDaoFactory.getStudentDao();
    //添加学生功能
    public boolean addStudent(Student stu) throws FileNotFoundException {
        return studentDao.addStudent(stu);
    }
    //判断是否存在
    public boolean isExists(String id) {
        Student[] newStu = studentDao.findAllStudent();
        boolean exists = false;
        for (int i = 0; i < newStu.length; i++) {
            Student stu = newStu[i];
            if(stu != null && stu.getId().equals(id)){
                exists = true;
                break;
            }
        }
        return  exists;
    }
    //查询所有学生
    public Student[] findAllStudent() {
        Student[] students = studentDao.findAllStudent();
        //标记位思想
        boolean flag = false;
        for (int i = 0; i < students.length; i++) {
            if(students[i] != null){
                flag = true;
            }
        }
        if(!flag){
            return null;
        }else{
            return students;
        }
    }
    //删除方法
    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }
    //修改方法
    public void updateStundet(Student stu) {
        studentDao.updateStudent(stu);
    }
}
